package service

import (
	"github.com/gin-gonic/gin"
	//"github.com/asaskevich/govalidator"
	//"github.com/gorilla/websocket"
	"go_part/models"
	"strconv"
	//"fmt"
	//"math/rand"
	//"go_part/utils"
	//"net/http"
	//"time"
  )

// GetApprovalTypeList
// @Tags         获得公司审批流程列表
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/GetApprovalTypeList [get]
func GetApprovalTypeList(c *gin.Context) {

	data := make([]*models.ApprovalType,10)
	data = models.GetApprovalTypeList()

	c.JSON(200, gin.H{
		"message": data,
	  })
}

// CreateApprovalType
// @Tags         创建审批类型
// @Param name query string false "审批名"
// @Param CompanyID query string false "公司ID"
// @Param DeviceInfo query string false "描述"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/CreateApprovalType [get]
func CreateApprovalType(c *gin.Context) {

	user := models.ApprovalType{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.DeviceInfo = c.Query("DeviceInfo")
	user.IsLogout = false

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	user.ApprovalID = strconv.Itoa(generateRandomNumber());
	models.CreateApprovalType(user)
	c.JSON(200 ,gin.H{
		"message": "新增审批类型成功！",
	  })
}

// FindApprovalTypeByCompanyID
// @Tags         查找审批类型（根据层级ID）
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/FindApprovalTypeByCompanyID [get]
func FindApprovalTypeByCompanyID(c *gin.Context) {
 
	user := models.ApprovalType{}
	user.CompanyID = c.Query("CompanyID")

	data := make([]*models.ApprovalType,100)
	data = models.FindApprovalTypeByCompanyID(user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": data,
	  })
}

// DeleteApprovalType
// @Tags         删除审批类型
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/DeleteApprovalType [get]
func DeleteApprovalType(c *gin.Context) {

	user := models.ApprovalType{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	models.DeleteApprovalType(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "删除设备成功！",
	  })
}

// UpdateApprovalType
// @Tags         更新审批类型
// @Param id formData string false "id"
// @Param name formData string false "审批名"
// @Param CompanyID formData string false "公司ID"
// @Param DeviceInfo formData string false "描述"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/UpdateApprovalType [post]
func UpdateApprovalType(c *gin.Context) {

	user := models.ApprovalType{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.DeviceInfo = c.PostForm("DeviceInfo")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	data1 := models.FindApprovalTypeByIDAndCompanyID(user.ID,user.CompanyID)
	if data1.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "审批不存在",
		  })
		return
	}

	models.UpdateApprovalType(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "更新区域成功！",
	  })
}

// GetApprovalStepModuleList
// @Tags         获得公司审批模型列表
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/GetApprovalStepModuleList [get]
func GetApprovalStepModuleList(c *gin.Context) {

	data := make([]*models.ApprovalStepModule,10)
	data = models.GetApprovalStepModuleList()

	c.JSON(200, gin.H{
		"message": data,
	  })
}

// CreateApprovalStepModule
// @Tags         创建审批模型
// @Param name query string false "审批名"
// @Param CompanyID query string false "公司ID"
// @Param DeviceInfo query string false "描述"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/CreateApprovalStepModule [get]
func CreateApprovalStepModule(c *gin.Context) {

	user := models.ApprovalStepModule{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.DeviceInfo = c.Query("DeviceInfo")
	user.IsLogout = false

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	user.ApprovalID = strconv.Itoa(generateRandomNumber());
	models.CreateApprovalStepModule(user)
	c.JSON(200 ,gin.H{
		"message": "新增审批类型成功！",
	  })
}


// FindApprovalStepModuleByCompanyID
// @Tags         查找审批模型（根据层级ID）
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/FindApprovalStepModuleByCompanyID [get]
func FindApprovalStepModuleByCompanyID(c *gin.Context) {
 
	user := models.ApprovalStepModule{}
	user.CompanyID = c.Query("CompanyID")

	data := make([]*models.ApprovalStepModule,100)
	data = models.FindApprovalStepModuleByCompanyID(user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": data,
	  })
}

// DeleteApprovalStepModule
// @Tags         删除审批模型
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/DeleteApprovalStepModule [get]
func DeleteApprovalStepModule(c *gin.Context) {

	user := models.ApprovalStepModule{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	models.DeleteApprovalStepModule(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "删除设备成功！",
	  })
}

// UpdateApprovalStepModule
// @Tags         更新审批模型
// @Param id formData string false "id"
// @Param name formData string false "审批名"
// @Param CompanyID formData string false "公司ID"
// @Param DeviceInfo formData string false "描述"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/UpdateApprovalStepModule [post]
func UpdateApprovalStepModule(c *gin.Context) {

	user := models.ApprovalStepModule{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.DeviceInfo = c.PostForm("DeviceInfo")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	models.UpdateApprovalStepModule(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "更新区域成功！",
	  })
}


// GetApprovalStepList
// @Tags         获得公司审批模板步骤列表
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/GetApprovalStepList [get]
func GetApprovalStepList(c *gin.Context) {

	data := make([]*models.ApprovalStep,10)
	data = models.GetApprovalStepList()

	c.JSON(200, gin.H{
		"message": data,
	  })
}

// CreateApprovalStep
// @Tags         创建审批步骤
// @Param name query string false "审批名"
// @Param CompanyID query string false "公司ID"
// @Param DeviceInfo query string false "描述"
// @Param StepNo query string false "步骤编号"
// @Param ApprovalModuleID query string false "审批模型ID"
// @Param DeparkmentID query string false "部门ID"
// @Param LevelLocalID query string false "区域ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/CreateApprovalStep [get]
func CreateApprovalStep(c *gin.Context) {

	user := models.ApprovalStep{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.DeviceInfo = c.Query("DeviceInfo")
	user.ApprovalModuleID = c.Query("ApprovalModuleID")
	user.DeparkmentID = c.Query("DeparkmentID")
	user.LevelLocalID = c.Query("LevelLocalID")
	user.StepNo = c.Query("StepNo")
	user.IsLogout = false

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	data1 := models.FindDeparkmentBasicByIDAndCompany(user.DeparkmentID,user.CompanyID)
	if data1.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "部门不存在",
		  })
		return
	}

	data2 := models.FindLevelLocalByIDAndCompanyID(user.LevelLocalID,user.CompanyID)
	if data2.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "区域不存在",
		  })
		return
	}

	user.StepID = strconv.Itoa(generateRandomNumber());
	models.CreateApprovalStep(user)
	c.JSON(200 ,gin.H{
		"message": "新增审批步骤成功！",
	  })
}

// FindApprovalStepByCompanyID
// @Tags         查找审批步骤（根据公司ID）
// @Param CompanyID query string false "公司ID"
// @Param ApprovalModuleID query string false "审批模型ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/FindApprovalStepByCompanyID [get]
func FindApprovalStepByCompanyID(c *gin.Context) {
 
	user := models.ApprovalStep{}
	user.CompanyID = c.Query("CompanyID")
	user.ApprovalModuleID = c.Query("ApprovalModuleID")

	data := make([]*models.ApprovalStep,100)
	data = models.FindApprovalStepByCompanyID(user.CompanyID,user.ApprovalModuleID)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": data,
	  })
}

// DeleteApprovalStep
// @Tags         删除审批步骤
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/DeleteApprovalStep [get]
func DeleteApprovalStep(c *gin.Context) {

	user := models.ApprovalStep{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	models.DeleteApprovalStep(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "删除审批步骤成功！",
	  })
}

// UpdateApprovalStep
// @Tags         更新审批步骤
// @Param id formData string false "id"
// @Param name formData string false "审批名"
// @Param CompanyID formData string false "公司ID"
// @Param DeparkmentID formData string false "部门ID"
// @Param LevelLocalID formData string false "区域ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/UpdateApprovalStep [post]
func UpdateApprovalStep(c *gin.Context) {

	user := models.ApprovalStep{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.DeviceInfo = c.PostForm("DeviceInfo")
	user.DeparkmentID = c.PostForm("DeparkmentID")
	user.LevelLocalID = c.PostForm("LevelLocalID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	models.UpdateApprovalStep(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "更新区域成功！",
	  })
}


// GetApprovalLocalList
// @Tags         获得公司审批流程实例列表
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/GetApprovalLocalList [get]
func GetApprovalLocalList(c *gin.Context) {

	data := make([]*models.ApprovalLocal,10)
	data = models.GetApprovalLocalList()

	c.JSON(200, gin.H{
		"message": data,
	  })
}

// CreateApprovalLocal
// @Tags         创建审批流程实例
// @Param name query string false "审批实例名称"
// @Param CompanyID query string false "公司ID"
// @Param DeviceInfo query string false "描述"
// @Param ApprovalID query string false "审批ID"
// @Param StepID query string false "步骤ID"
// @Param IdentityID query string false "审批人ID"
// @Param Action query string false "动作"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/CreateApprovalLocal [get]
func CreateApprovalLocal(c *gin.Context) {

	user := models.ApprovalLocal{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.DeviceInfo = c.Query("DeviceInfo")
	user.ApprovalID = c.Query("ApprovalID")
	user.StepID = c.Query("StepID")
	user.IdentityID = c.Query("IdentityID")
	user.Action = c.Query("Action")
	user.IsDone = false

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	data1 := models.FindApprovalTypeByDeviceIDAndCompanyID(user.ApprovalID,user.CompanyID)
	if data1.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "审批不存在",
		  })
		return
	}

	data2 := models.FindApprovalStepByStepID(user.StepID,user.CompanyID)
	if data2.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "审批步骤不存在",
		  })
		return
	}
	

	id,_ := strconv.Atoi(user.IdentityID)
	data3 := models.FindUserByID(uint(id))
	if data3.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "用户不存在",
		  })
		return
	}

	user.StepLocalID = strconv.Itoa(generateRandomNumber());
	models.CreateApprovalLocal(user)
	c.JSON(200 ,gin.H{
		"message": "新增审批步骤成功！",
	  })
}

// FindApprovalLocalByApprovalID
// @Tags         查找审批流程实例（根据层级ID）
// @Param CompanyID query string false "公司ID"
// @Param ApprovalID query string false "审批ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/FindApprovalLocalByApprovalID [get]
func FindApprovalLocalByApprovalID(c *gin.Context) {
 
	user := models.ApprovalLocal{}
	user.CompanyID = c.Query("CompanyID")
	user.ApprovalID = c.Query("ApprovalID")

	data := make([]*models.ApprovalLocal,100)
	data = models.FindApprovalLocalByStepID(user.ApprovalID,user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": data,
	  })
}

// DeleteApprovalLocal
// @Tags         删除审批流程实例
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/DeleteApprovalLocal [get]
func DeleteApprovalLocal(c *gin.Context) {

	user := models.ApprovalLocal{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	models.DeleteApprovalLocal(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "删除审批步骤实例成功！",
	  })
}

// UpdateApprovalLocal
// @Tags         更新审批流程实例
// @Param id formData string false "id"
// @Param name formData string false "审批实例名称"
// @Param CompanyID formData string false "公司ID"
// @Param DeviceInfo formData string false "描述"
// @Param Identity formData string false "审批人ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/UpdateApprovalLocal [post]
func UpdateApprovalLocal(c *gin.Context) {

	user := models.ApprovalLocal{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.DeviceInfo = c.PostForm("DeviceInfo")
	user.IdentityID = c.PostForm("Identity")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	id,_ = strconv.Atoi(user.IdentityID)
	data3 := models.FindUserByID(uint(id))
	if data3.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "用户不存在",
		  })
		return
	}

	data2 := models.FindApprovalLocalByIDAndCompanyID(user.ID,user.CompanyID)
	if data2.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "审批不存在",
		  })
		return
	}

	models.UpdateApprovalLocal(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "更新审批成功！",
	  })
}


// GetApprovalSelfDefineList
// @Tags         获得公司审批步骤实例列表
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/GetApprovalSelfDefineList [get]
func GetApprovalSelfDefineList(c *gin.Context) {

	data := make([]*models.ApprovalSelfDefine,10)
	data = models.GetApprovalSelfDefineList()

	c.JSON(200, gin.H{
		"message": data,
	  })
}

// CreateApprovalSelfDefine
// @Tags         创建自定义审批步骤流程实例
// @Param name query string false "审批实例名称"
// @Param CompanyID query string false "公司ID"
// @Param DeviceInfo query string false "描述"
// @Param StepNo query string false "步骤序号"
// @Param ApprovalID query string false "审批类型ID"
// @Param IdentityID query string false "审批人ID"
// @Param Action query string false "动作"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/CreateApprovalSelfDefine [get]
func CreateApprovalSelfDefine(c *gin.Context) {

	user := models.ApprovalSelfDefine{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.DeviceInfo = c.Query("DeviceInfo")
	step, _ := strconv.Atoi(c.Query("StepNo"))
	user.StepNo = int64(step)
	user.ApprovalID = c.Query("ApprovalID")
	user.IdentityID = c.Query("IdentityID")
	user.Action = c.Query("Action")
	user.IsDone = false

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	id,_ := strconv.Atoi(user.IdentityID)
	data1 := models.FindUserByID(uint(id))
	if data1.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "用户不存在",
		  })
		return
	}

	user.StepLocalID = strconv.Itoa(generateRandomNumber());
	models.CreateApprovalSelfDefine(user)
	c.JSON(200 ,gin.H{
		"message": "新增审批步骤实例成功！",
	  })
}

// FindApprovalSelfDefineByStepID
// @Tags         查找自定义审批流程实例（根据层级ID）
// @Param CompanyID query string false "公司ID"
// @Param ApprovalID query string false "审批ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/FindApprovalSelfDefineByStepID [get]
func FindApprovalSelfDefineByStepID(c *gin.Context) {
 
	user := models.ApprovalSelfDefine{}
	user.CompanyID = c.Query("CompanyID")
	user.ApprovalID = c.Query("ApprovalID")

	data := make([]*models.ApprovalSelfDefine,100)
	data = models.FindApprovalSelfDefineByStepID(user.ApprovalID,user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": data,
	  })
}

// DeleteApprovalSelfDefine
// @Tags         删除自定义审批流程实例
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/DeleteApprovalSelfDefine [get]
func DeleteApprovalSelfDefine(c *gin.Context) {

	user := models.ApprovalSelfDefine{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	models.DeleteApprovalSelfDefine(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "删除审批步骤实例成功！",
	  })
}

// UpdateApprovalSelfDefine
// @Tags         更新自定义审批流程实例
// @Param id formData string false "id"
// @Param name formData string false "审批实例名称"
// @Param CompanyID formData string false "公司ID"
// @Param DeviceInfo formData string false "描述"
// @Param Identity formData string false "审批人ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /approval/UpdateApprovalSelfDefine [post]
func UpdateApprovalSelfDefine(c *gin.Context) {

	user := models.ApprovalSelfDefine{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.DeviceInfo = c.PostForm("DeviceInfo")
	user.IdentityID = c.PostForm("Identity")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	user_id,_ := strconv.Atoi(user.IdentityID)
	data1 := models.FindUserByID(uint(user_id))
	if data1.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "用户不存在",
		  })
		return
	}

	data2 := models.FindApprovalSelfDefineByIDAndCompanyID(user.ID,user.CompanyID)
	if data2.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "审批不存在",
		  })
		return
	}

	models.UpdateApprovalSelfDefine(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "更新审批步骤实例成功！",
	  })
}
